package com.lagou.handler;

import com.lagou.util.ZkCuratorUtils;

import java.util.concurrent.Callable;

public class DurationCounterHandler implements Callable {

    private ZkCuratorUtils zkClient;

    public DurationCounterHandler(ZkCuratorUtils zkClient) {
        this.zkClient = zkClient;
    }

    private long duration;

    private String hostAndPort;

    public String getHostAndPort() {
        return hostAndPort;
    }

    public void setHostAndPort(String hostAndPort) {
        this.hostAndPort = hostAndPort;
    }

    public long getDuration() {
        return duration;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    /**
     * 记录响应时间
     *
     * @return
     * @throws Exception
     */
    @Override
    public Object call() throws Exception {
//        InetAddress address = InetAddress.getLocalHost();
        if (duration < 5000l) {
            zkClient.writeData("/" + hostAndPort, String.valueOf(duration));
        } else {
            zkClient.writeData("/" + hostAndPort, "-1");
        }
        return null;
    }

}
